/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> qu = new LinkedList<>();
        List<List<Integer>> list = new ArrayList<>();
        if(root == null) return list;
        qu.offer(root);
        while(!qu.isEmpty()){
            List<Integer> tmp = new ArrayList<>();
            for(int i = qu.size(); i > 0; i--){
                TreeNode node = qu.poll();
                tmp.add(node.val);
                if(node.left != null) qu.offer(node.left);
                if(node.right != null) qu.offer(node.right);
            }
            list.add(tmp);
        }
        return list;
    }
}

